package com.jxbeidou.system.log.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.log.convert.LogLoginConvert;
import com.jxbeidou.system.log.entity.LogLoginDO;
import com.jxbeidou.system.log.service.LogLoginService;
import com.jxbeidou.system.log.query.LogLoginQuery;
import com.jxbeidou.system.api.log.vo.LogLoginVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.List;

/**
* 登录日志
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/log-login")
@AllArgsConstructor
@Tag(name = "登录日志接口")
public class LogLoginController {
    @Resource
    private LogLoginService logLoginService;

    @PostMapping("/page")
    @Operation(summary = "查询详情")
    public R<PageResult<LogLoginVO>> page(@RequestBody @Valid LogLoginQuery query){
        PageResult<LogLoginVO> page = logLoginService.page(query);

        return R.success(page);
    }

    @GetMapping("/{id}")
    @Operation(summary = "按照ID查询")
    public R<LogLoginVO> get(@PathVariable("id") Long id){
        LogLoginDO entity = logLoginService.getById(id);

        return R.success(LogLoginConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存记录")
    public R<String> save(@RequestBody LogLoginVO vo){
        logLoginService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改记录")
    public R<String> update(@RequestBody @Valid LogLoginVO vo){
        logLoginService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/id")
    @Operation(summary = "删除指定的记录")
    public R<String> delete(@PathVariable("id") Long id){
        logLoginService.delete(Arrays.asList(id));
        return R.deleteSuccess();
    }

    @PutMapping("delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody List<Long> idList) {
        logLoginService.delete(idList);
        return R.deleteSuccess();
    }
}